package com.example.config;

import com.example.filter.SecurityFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.servlet.Filter;

/**
 * @Author:tjf
 * @Description:
 */
@Configuration
public class ServletConfig {
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 把自定义的过滤器添加到IOC容器中
     * @return
     */
    @Bean
    public FilterRegistrationBean securityFilter(){
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        // 创建自定义的过滤器
        SecurityFilter securityFilter = new SecurityFilter();
        securityFilter.setRedisTemplate(redisTemplate);
        // 注册过滤器
        filterFilterRegistrationBean.setFilter(securityFilter);
        // 定义过滤的路径
        filterFilterRegistrationBean.addUrlPatterns("/*");
        return filterFilterRegistrationBean;
    }
}
